/**
 * 
 */
package net.toocruel.iqismart.web.init;

import net.toocruel.iqismart.dao.ResourceRepository;
import net.toocruel.iqismart.dao.RoleRepository;
import net.toocruel.iqismart.dao.UserRepository;
import net.toocruel.iqismart.dao.core.CategoryRepository;
import net.toocruel.iqismart.dao.core.TagRepository;
import net.toocruel.iqismart.entity.common.Category;
import net.toocruel.iqismart.entity.common.Tag;
import net.toocruel.iqismart.entity.constants.CategoryVisibility;
import net.toocruel.iqismart.entity.constants.ResourceType;
import net.toocruel.iqismart.entity.constants.ReviewStatus;
import net.toocruel.iqismart.entity.rbac.Resource;
import net.toocruel.iqismart.entity.rbac.Role;
import net.toocruel.iqismart.entity.rbac.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 默认的系统数据初始化器，永远在其他数据初始化器之前执行
 * 
 * @author toocruel
 *
 */
@Component
public class AdminDataInitializer extends AbstractDataInitializer {

	@Autowired
	private PasswordEncoder passwordEncoder;

	@Autowired
	private RoleRepository roleRepository;

	@Autowired
	private UserRepository userRepository;

	@Autowired
	private CategoryRepository categoryRepository;

	@Autowired
	private TagRepository tagRepository;

	@Autowired
	protected ResourceRepository resourceRepository;


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.idea.core.spi.initializer.DataInitializer#getIndex()
	 */
	@Override
	public Integer getIndex() {
		return Integer.MIN_VALUE;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.idea.core.spi.initializer.AbstractDataInitializer#doInit()
	 */
	@Override
	protected void doInit() {
		initResource();
		initRole();
		initUser();
		initCommonUser();
		initTestUser();
		initCategoty();
		initTag();
	}

	private void initTag() {

		Tag tag = new Tag();
		tag.setName("互联网");
		tag.setCategory(categoryRepository.findBySlug("default"));
		tagRepository.save(tag);

		tag = new Tag();
		tag.setName("springboot");
		tag.setCategory(categoryRepository.findBySlug("default"));
		tagRepository.save(tag);

		tag = new Tag();
		tag.setName("程序发布");
		tag.setCategory(categoryRepository.findBySlug("default"));
		tagRepository.save(tag);

		for(int i=0;i<10;i++){
			tag = new Tag();
			tag.setName("测试话题"+i);
			tag.setCategory(categoryRepository.findBySlug("feedback"));
			tagRepository.save(tag);
		}

		for(int i=0;i<10;i++){
			tag = new Tag();
			tag.setName("测试话题"+i);
			tag.setCategory(categoryRepository.findBySlug("using"));
			tagRepository.save(tag);
		}

		for(int i=0;i<10;i++){
			tag = new Tag();
			tag.setName("测试话题"+i);
			tag.setCategory(categoryRepository.findBySlug("help"));
			tagRepository.save(tag);
		}

	}

	private void initCategoty() {
		Category category = new Category();
		category.setName("默认分类");
		category.setSlug("default");
		category.setGrade(0);
		category.setSort(0);
		category.setStatus(ReviewStatus.REVIEW_PASS);
		categoryRepository.save(category);

		category = new Category();
		category.setName("反馈建议");
		category.setSlug("feedback");
		category.setGrade(1);
		category.setSort(1);
		category.setVisibilities(CategoryVisibility.init(CategoryVisibility.questions));
		category.setStatus(ReviewStatus.REVIEW_PASS);
		categoryRepository.save(category);

		category = new Category();
		category.setName("程序使用");
		category.setSlug("using");
		category.setGrade(2);
		category.setSort(2);
		category.setStatus(ReviewStatus.REVIEW_PASS);
		categoryRepository.save(category);

		category = new Category();
		category.setName("帮助文档");
		category.setSlug("help");
		category.setGrade(3);
		category.setSort(3);
		category.setStatus(ReviewStatus.REVIEW_PASS);
		categoryRepository.save(category);
	}

	/**
	 * 初始化用户数据
	 */
	private void initUser() {
		User user = new User();
		user.setUsername("admin");
		user.setPassword(passwordEncoder.encode("111111"));
		user.setAvatar("https://avatars3.githubusercontent.com/u/8500372?v=4&s=120");
		Collection<Role> roles = roleRepository.findByIdIn(new Long[]{2L});
		user.setRoles(roles.stream().collect(Collectors.toSet()));
		userRepository.save(user);

		User user2 = new User();
		user2.setUsername("admin2");
		user2.setPassword(passwordEncoder.encode("111111"));
		user2.setAvatar("https://avatars3.githubusercontent.com/u/8500372?v=4&s=120");
		Collection<Role> roles2 = roleRepository.findByIdIn(new Long[]{2L});
		user2.setRoles(roles2.stream().collect(Collectors.toSet()));
		userRepository.save(user2);
	}

	private void initCommonUser(){
		List<User> userList = new ArrayList<>();
		for(int i=0;i<=3;i++){
			User user = new User();
			user.setUsername("user"+i);
			user.setPassword(passwordEncoder.encode("111111"));
			user.setAvatar("https://avatars3.githubusercontent.com/u/8500372?v=4&s=120");
			Collection<Role> roles = roleRepository.findByIdIn(new Long[]{1L});
			user.setRoles(roles.stream().collect(Collectors.toSet()));
			userList.add(user);
		}
		userRepository.save(userList);
	}

	private void initTestUser(){
		List<User> userList = new ArrayList<>();
		for(int i=0;i<=3;i++){
			User user = new User();
			user.setUsername("test"+i);
			user.setPassword(passwordEncoder.encode("111111"));
			user.setAvatar("https://avatars2.githubusercontent.com/u/23741155?v=4");
			Collection<Role> roles = roleRepository.findByIdIn(new Long[]{1L});
			user.setRoles(roles.stream().collect(Collectors.toSet()));
			userList.add(user);
		}
		userRepository.save(userList);
	}

	/**
	 * 初始化角色数据
	 */
	private void initRole() {
		Role commonRole = new Role(1L, "普通用户");
		Role adminRole = new Role(2L, "超级管理员");
		Role[] roles= new Role[]{
				commonRole,
				adminRole
		};
		List<Resource> all = resourceRepository.findAll();
		adminRole.setResources(all.stream().collect(Collectors.toSet()));
		Arrays.stream(roles).forEach(role->{
			roleRepository.save(role);
		});

	}

	/**
	 * 初始化菜单数据
	 */
	protected void initResource() {
		//创建根节点
		Resource root = new Resource("根节点", "#", "", null, null, null);
		resourceRepository.save(root);
		//模块划分
		{
			//首页
			Resource index = new Resource("首页", "/admin/index", "dashboard", null, Arrays.asList("/admin/index","/admin","/admin/").stream().collect(Collectors.toSet()), root);
			resourceRepository.save(index);
		}
		{
			//全局
			Resource menu1 = new Resource("全局", "#", "wrench", null, null, root);
			resourceRepository.save(menu1);

			resourceRepository.save(new Resource("站点信息","/admin/setting/website","",ResourceType.MENU,Arrays.asList("/admin/setting/website").stream().collect(Collectors.toSet()),menu1));
			resourceRepository.save(new Resource("邮箱设置","/admin/setting/email","",ResourceType.MENU,Arrays.asList("/admin/setting/email").stream().collect(Collectors.toSet()),menu1));
			resourceRepository.save(new Resource("注册设置","/admin/setting/register","",ResourceType.MENU,Arrays.asList("/admin/setting/register").stream().collect(Collectors.toSet()),menu1));
			resourceRepository.save(new Resource("时间设置","/admin/setting/time","",ResourceType.MENU,Arrays.asList("/admin/setting/time").stream().collect(Collectors.toSet()),menu1));
			resourceRepository.save(new Resource("防灌水设置","/admin/setting/irrigation","",ResourceType.MENU,Arrays.asList("/admin/setting/irrigation").stream().collect(Collectors.toSet()),menu1));
			resourceRepository.save(new Resource("积分设置","/admin/setting/credits","",ResourceType.MENU,Arrays.asList("/admin/setting/credits").stream().collect(Collectors.toSet()),menu1));
			resourceRepository.save(new Resource("SEO设置","/admin/setting/seo","",ResourceType.MENU,Arrays.asList("/admin/setting/seo").stream().collect(Collectors.toSet()),menu1));
			resourceRepository.save(new Resource("系统工具","/admin/system/index","",ResourceType.MENU,Arrays.asList("/admin/system/index").stream().collect(Collectors.toSet()),menu1));

		}

		{
			//用户
			Resource menu2 = new Resource("用户", "#", "users", null, null, root);
			resourceRepository.save(menu2);

			//resourceRepository.save(new Resource("资源管理","","",ResourceType.MENU,Arrays.asList("/admin/system/resource").stream().collect(Collectors.toSet()),system));
			resourceRepository.save(new Resource("用户管理","/admin/user","",ResourceType.MENU,Arrays.asList("/admin/user").stream().collect(Collectors.toSet()),menu2));
			resourceRepository.save(new Resource("用户组","/admin/role","",ResourceType.MENU,Arrays.asList("/admin/role").stream().collect(Collectors.toSet()),menu2));
			resourceRepository.save(new Resource("专家管理","/admin/authentication","",ResourceType.MENU,Arrays.asList("/admin/authentication").stream().collect(Collectors.toSet()),menu2));
		}

		{
			//内容
			Resource menu3 = new Resource("内容", "#", "comments-o", null, null, root);
			resourceRepository.save(menu3);

			resourceRepository.save(new Resource("问题管理","/admin/question","",ResourceType.MENU,Arrays.asList("/admin/question").stream().collect(Collectors.toSet()),menu3));
			resourceRepository.save(new Resource("回答管理","/admin/answer","",ResourceType.MENU,Arrays.asList("/admin/answer").stream().collect(Collectors.toSet()),menu3));
			resourceRepository.save(new Resource("文章管理","/admin/article","",ResourceType.MENU,Arrays.asList("/admin/article").stream().collect(Collectors.toSet()),menu3));
			resourceRepository.save(new Resource("评论管理","/admin/comment","",ResourceType.MENU,Arrays.asList("/admin/comment").stream().collect(Collectors.toSet()),menu3));
			resourceRepository.save(new Resource("标签管理","/admin/tag","",ResourceType.MENU,Arrays.asList("/admin/tag").stream().collect(Collectors.toSet()),menu3));
			resourceRepository.save(new Resource("分类管理","/admin/category","",ResourceType.MENU,Arrays.asList("/admin/category").stream().collect(Collectors.toSet()),menu3));
		}

		{
			//运营
			Resource menu4 = new Resource("运营", "#", "cutlery", null, null, root);
			resourceRepository.save(menu4);

			resourceRepository.save(new Resource("公共管理","/admin/notice","",ResourceType.MENU,Arrays.asList("/admin/notice").stream().collect(Collectors.toSet()),menu4));
			resourceRepository.save(new Resource("首页推荐","/admin/recommendation","",ResourceType.MENU,Arrays.asList("/admin/recommendation").stream().collect(Collectors.toSet()),menu4));
			resourceRepository.save(new Resource("积分商城","/admin/goods","",ResourceType.MENU,Arrays.asList("/admin/goods").stream().collect(Collectors.toSet()),menu4));
			resourceRepository.save(new Resource("兑换记录","/admin/exchange","",ResourceType.MENU,Arrays.asList("/admin/exchange").stream().collect(Collectors.toSet()),menu4));
			resourceRepository.save(new Resource("友情链接","/admin/friendLink","",ResourceType.MENU,Arrays.asList("/admin/friendLink").stream().collect(Collectors.toSet()),menu4));
		}

		{
			//财务
			Resource menu5 = new Resource("财务", "#", "database", null, null, root);
			resourceRepository.save(menu5);

			resourceRepository.save(new Resource("积分管理","/admin/credit","",ResourceType.MENU,Arrays.asList("/admin/credit").stream().collect(Collectors.toSet()),menu5));
		}

		{
			//第三方
			Resource menu6 = new Resource("第三方", "#", "recycle", null, null, root);
			resourceRepository.save(menu6);

			resourceRepository.save(new Resource("搜索设置","/admin/setting/search","",ResourceType.MENU,Arrays.asList("/admin/setting/search").stream().collect(Collectors.toSet()),menu6));
			resourceRepository.save(new Resource("一键登录","/admin/setting/oauth","",ResourceType.MENU,Arrays.asList("/admin/setting/oauth").stream().collect(Collectors.toSet()),menu6));
		}


	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.idea.core.spi.initializer.AbstractDataInitializer#isNeedInit()
	 */
	@Override
	protected boolean isNeedInit() {
		return userRepository.count() == 0;
	}

}
